package com.mantru.iotpot.dishes;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.Cook.FinishFoodActivity;
import com.mantru.iotpot.Cook.PutFoodActivity;
import com.mantru.iotpot.R;

public class DishesPutFoodActivity extends AppCompatActivity {
    private ApplicationClass ac;
    private Handler handler;
    private Runnable runnable;
    private int countdown = 60; // 初始倒计时为60秒
    private int vegetableId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dishes_put_food);
        ac = (ApplicationClass) getApplication(); // 初始化 ApplicationClass
//        boolean result = ac.Cooking_PourOut();
        Intent intent = getIntent();
        vegetableId = intent.getIntExtra("vegetableId",-1);
        // 初始化倒计时文本
        TextView countdownTextView = findViewById(R.id.wait_finish_secound);
        countdownTextView.setText(countdown + "S");
        // 初始化 Handler 和 Runnable
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                // 每秒更新倒计时
                countdown--;
                if (countdown >= 0) {
                    runOnUiThread(() -> {
                        TextView countdownTextView = findViewById(R.id.wait_finish_secound);
                        countdownTextView.setText(countdown + "S");
                    });
                    Log.d("putfood","ac.pot_position_value: " + ac.pot_position_value);
                    // 判断 pot_position_value 是否为 2
                    if (ac.pot_position_value == 2) {
                        // 跳转到下一个界面
                        Intent intent = new Intent(DishesPutFoodActivity.this, DishesCreateActivity.class);
                        intent.putExtra("vegetableId",vegetableId);
                        startActivity(intent);
                        finish(); // 关闭当前活动
                    } else if (countdown == 0) {
                        // 倒计时结束，跳转到下一个界面
                        Intent intent = new Intent(DishesPutFoodActivity.this, DishesCreateActivity.class);
                        intent.putExtra("vegetableId",vegetableId);
                        startActivity(intent);
                        finish(); // 关闭当前活动
                    } else {
                        // 继续倒计时
                        handler.postDelayed(this, 1000);
                    }
                }
            }
        };

        // 启动倒计时
        handler.postDelayed(runnable, 1000);
    }

    @Override
    protected void onDestroy() {
        // 移除所有回调，避免内存泄漏
        if (handler != null) {
            handler.removeCallbacks(runnable);
        }
        super.onDestroy();
    }
}